package com.avcdata.national.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.national.module.AllBrands;
import com.avcdata.national.module.BrandsPageDatakInfo;

import java.util.List;

/**
 * 添加竞品适配器
 *
 * @author Administrator
 */
public class PersonalAddProductAdapter extends BaseAdapter implements SectionIndexer {

    private Activity context;
    private List<BrandsPageDatakInfo> list;

    public PersonalAddProductAdapter(Activity context, List<BrandsPageDatakInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size ();
    }

    @Override
    public Object getItem(int position) {
        return list.get ( position );
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<BrandsPageDatakInfo> list) {
        this.list = list;
        notifyDataSetChanged ();
    }

    @Override
    public View getView(int position, View converView, ViewGroup arg2) {
        ViewHolder holder;
        if (converView == null) {
            converView = LayoutInflater.from ( context ).inflate ( R.layout.item_personal_addproduct, null );
            holder = new ViewHolder ();
            holder.tv_category = (TextView) converView.findViewById ( R.id.personal_addproduct_item_tv );
            holder.iv_right = (ImageView) converView.findViewById ( R.id.personal_addproduct_item_iv );
            holder.tvLetter = (TextView) converView.findViewById ( R.id.catalog );
            converView.setTag ( holder );
        } else {
            holder = (ViewHolder) converView.getTag ();
        }

        //根据position获取分类的首字母的char ascii值
        int section = getSectionForPosition ( position );

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection ( section )) {
            holder.tvLetter.setVisibility ( View.VISIBLE );
            holder.tvLetter.setText ( list.get ( position ).getSortLetters () );
        } else {
            holder.tvLetter.setVisibility ( View.GONE );
        }

        holder.tv_category.setText ( list.get ( position ).getBrands () );
        if (list.get ( position ).isSelected ()) {
            holder.iv_right.setVisibility ( View.VISIBLE );
        } else {
            holder.iv_right.setVisibility ( View.GONE );
        }
        return converView;
    }

    class ViewHolder {
        private TextView tv_category;
        private ImageView iv_right;
        private TextView tvLetter;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get ( position ).getSortLetters ().charAt ( 0 );
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount (); i++) {
            String sortStr = list.get ( i ).getSortLetters ();
            char firstChar = sortStr.toUpperCase ().charAt ( 0 );
            if (firstChar == section) {
                return i;
            }
        }

        return -1;
    }

    @Override
    public Object[] getSections() {
        return null;
    }

}
